<?php

namespace App\Manage;

use App\Models\SiteTag;
use App\Service\Base\Manage;
use App\Service\Translate;

class SiteTagManage extends Manage
{
    // 标签
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteTag';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 软删除
    // 启用、停用


    // 列表查询
    protected function listWhere()
    {
        $opt      = $this->option();
        $space    = $opt->getData('space', 0, 'intval');
        $cate_id  = $opt->getData('cate_id', 0, 'intval');
        $terminal = $opt->getData('terminal', null, 'trim');
        $keyword  = $opt->getData('keyword', null, 'trim');

        $where = [['status', '>=', 0]];
        $bang  = [];
        if($terminal){
            $bang[]  = 'terminal';
            $where[] = ['terminal', '=', $terminal];
        }
        if($keyword){
            $bang[]  = false;
            $where[] = $this->orWhere('en_title|cn_title|tc_title', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }

    // 添加
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            'rank'     => $opt->getData('rank', 0, 'intval'),
            'en_title' => $opt->getData('en_title', null, 'trim'),
            'cn_title' => $opt->getData('cn_title', null, 'trim'),
            'tc_title' => $opt->getData('tc_title', null, 'trim'),
            'status'   => $opt->getData('status', 1, 'intval'),
        ];

        // 条件
        $where = [];
        $has   = '';
        $nos   = [];
        if(!empty($data['en_title'])){
            $has               = 'en';
            $where['en_title'] = $data['en_title'];
        } else{
            $nos[] = 'en';
        }
        if(!empty($data['cn_title'])){
            $has               = 'cn';
            $where['cn_title'] = $data['cn_title'];
        } else{
            $nos[] = 'cn';
        }
        /*if(!empty($data['tc_title'])){
            $has               = 'tc';
            $where['tc_title'] = $data['tc_title'];
        } else{
            $nos[] = 'tc';
        }*/
        if(empty($where)){
            return $this->_returnFail('请传递标签字段的值！');
        }

        // 查询标签是否存在
        $tagDb = new SiteTag();
        $info  = $tagDb->where($where)->first();
        if($info){
            return $this->_returnFail('该标签已经存在！');
        }

        // 如果没有 则翻译
        $translate = new Translate();
        foreach($nos as $_no){
            $result = $translate->translateTo($data[$has.'_title'], $_no);
            $text   = $result['text'] ?? '';
            // 组合翻译后的数据
            $data[$_no.'_title'] = $text;
        }

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            'rank'     => $opt->getData('rank', 0, 'intval'),
            'en_title' => $opt->getData('en_title', null, 'trim'),
            'cn_title' => $opt->getData('cn_title', null, 'trim'),
            'tc_title' => $opt->getData('tc_title', null, 'trim'),
            //'status'   => $opt->getData('status', 1, 'intval'),
        ];

        // 条件
        $where = [];
        $has   = '';
        $nos   = [];
        if(!empty($data['en_title'])){
            $has               = 'en';
            $where['en_title'] = $data['en_title'];
        } else{
            $nos[] = 'en';
        }
        if(!empty($data['cn_title'])){
            $has               = 'cn';
            $where['cn_title'] = $data['cn_title'];
        } else{
            $nos[] = 'cn';
        }
        if(!empty($data['tc_title'])){
            $has               = 'tc';
            $where['tc_title'] = $data['tc_title'];
        } else{
            $nos[] = 'tc';
        }
        if(empty($where)){
            return $this->_returnFail('请传递标签字段的值！');
        }

        // 查询标签是否存在
        $id    = $opt->getData('id', 0, 'intval');
        $tagDb = new SiteTag();
        $info  = $tagDb->where($where)->where('id', '<>', $id)->first();
        if($info){
            return $this->_returnFail('该标签已经存在！');
        }


        // 如果没有 则翻译
        $translate = new Translate();
        foreach($nos as $_no){
            $result = $translate->translateTo($data[$has.'_title'], $_no);
            // 组合翻译后的数据
            $data[$_no.'_title'] = $result['text'] ?? '';
        }

        return $data;
    }

}


